<template>
	<view class="performance-container">
		<!-- 时间筛选 -->
		<view class="time-filter">
			<view class="filter-tabs">
				<view class="filter-tab" v-for="(period, index) in timePeriods" :key="index"
					:class="{ active: currentPeriod === period.value }" @click="switchPeriod(period.value)">
					<text class="tab-text">{{ period.label }}</text>
				</view>
			</view>
		</view>

		<!-- 业绩概览 -->
		<view class="overview-card">
			<view class="overview-header">
				<text class="overview-title">{{ currentPeriodLabel }}业绩概览</text>
				<!-- <text class="overview-date">{{ currentPeriodData.dateRange }}</text> -->
			</view>

			<view class="overview-stats">
				<view class="stat-item">
					<text class="stat-number">¥{{ currentPeriodData.direct_money }}</text>
					<text class="stat-label">总销售额</text>
					<view class="stat-trend" :class="[
						currentPeriodData.amountGrowth > 0 ? 'up' : '',
						currentPeriodData.amountGrowth < 0 ? 'down' : '',
						currentPeriodData.amountGrowth === 0 ? 'stable' : ''
					]">
						<text class="trend-text">{{ getTrendText(currentPeriodData.amountGrowth) }}</text>
						<text class="trend-percent">{{ currentPeriodData.amountGrowth }}%</text>
					</view>
				</view>
				<view class="stat-item">
					<text class="stat-number">{{ currentPeriodData.direct_salenum }}</text>
					<text class="stat-label">总销量</text>
					<view class="stat-trend" :class="[
						currentPeriodData.salesGrowth > 0 ? 'up' : '',
						currentPeriodData.salesGrowth < 0 ? 'down' : '',
						currentPeriodData.salesGrowth === 0 ? 'stable' : ''
					]">
						<text class="trend-text">{{ getTrendText(currentPeriodData.salesGrowth) }}</text>
						<text class="trend-percent">{{ currentPeriodData.salesGrowth }}%</text>
					</view>
				</view>
				<view class="stat-item">
					<text class="stat-number">{{ currentPeriodData.direct_addnum }}</text>
					<text class="stat-label">新增成员</text>
					<view class="stat-trend" :class="[
						currentPeriodData.addnumGrowth > 0 ? 'up' : '',
						currentPeriodData.addnumGrowth < 0 ? 'down' : '',
						currentPeriodData.addnumGrowth === 0 ? 'stable' : ''
					]">
						<text class="trend-text">{{ getTrendText(currentPeriodData.addnumGrowth) }}</text>
						<text class="trend-percent">{{ currentPeriodData.addnumGrowth }}%</text>
					</view>
				</view>
			</view>
		</view>

		<!-- 详细数据 -->
		<view class="detail-card">
			<view class="detail-header">
				<text class="detail-title">详细数据</text>
			</view>

			<view class="detail-grid">
				<view class="detail-item">
					<text class="detail-label">直推销售额</text>
					<text class="detail-value">¥{{ currentPeriodData.direct_money }}</text>
				</view>
				<view class="detail-item">
					<text class="detail-label">团队销售额</text>
					<text class="detail-value">¥{{ currentPeriodData.team_money }}</text>
				</view>
				<view class="detail-item">
					<text class="detail-label">直推销量</text>
					<text class="detail-value">{{ currentPeriodData.direct_salenum }}</text>
				</view>
				<view class="detail-item">
					<text class="detail-label">团队销量</text>
					<text class="detail-value">{{ currentPeriodData.team_salenum }}</text>
				</view>
				<view class="detail-item">
					<text class="detail-label">直推新增</text>
					<text class="detail-value">{{ currentPeriodData.direct_addnum }}</text>
				</view>
				<view class="detail-item">
					<text class="detail-label">团队新增</text>
					<text class="detail-value">{{ currentPeriodData.team_addnum }}</text>
				</view>
			</view>
		</view>

		<!-- 收益明细 -->
		<view class="income-card">
			<view class="income-header">
				<text class="income-title">收益明细</text>
				<text class="income-total">总收益：¥ {{ currentPeriodData.totalCommission }}</text>
			</view>

			<view class="income-list">
				<view class="income-item">
					<view class="income-info">
						<text class="income-type">结算佣金</text>
						<text class="income-desc">推荐用户购买获得的佣金</text>
					</view>
					<text class="income-amount">¥ {{ currentPeriodData.settleCommission }}</text>
				</view>
				<view class="income-item">
					<view class="income-info">
						<text class="income-type">在途佣金</text>
						<text class="income-desc">推荐用户购买获得的佣金</text>
					</view>
					<text class="income-amount">¥ {{ currentPeriodData.transitCommission }}</text>
				</view>
			</view>
		</view>

		<!-- 业绩趋势图 -->
		<view class="chart-section">
			<view class="stats-card">
				<view class="card-header">
					<text class="card-title">业绩趋势</text>
					<view class="chart-tabs">
						<view class="chart-tab" :class="{ active: chartType === 'sales' }"
							@click="switchChart('sales')">
							<text class="chart-tab-text">销量</text>
						</view>
						<view class="chart-tab" :class="{ active: chartType === 'amount' }"
							@click="switchChart('amount')">
							<text class="chart-tab-text">销售额</text>
						</view>
					</view>
				</view>

				<!-- 简化的图表展示 -->
				<view class="simple-chart">
					<view class="chart-bars">
						<view class="chart-bar" v-for="(item, index) in currentChartData" :key="index">
							<view class="bar-container">
								<view class="bar-fill" :style="{ height: getBarHeight(item.value) + '%' }"></view>
							</view>
							<text class="bar-label">{{ item.label }}</text>
							<text class="bar-value">{{ formatChartValue(item.value) }}</text>
						</view>
					</view>
				</view>
			</view>
		</view>

		<!-- 排名信息 -->
		<view class="ranking-card">
			<view class="ranking-header">
				<text class="ranking-title">我的排名</text>
			</view>

			<view class="ranking-list">
				<view class="ranking-item">
					<text class="ranking-label">销售额排名</text>
					<view class="ranking-value">
						<text class="ranking-number">{{ currentPeriodData.userSort1 }}</text>
						<text class="ranking-total">/{{ currentPeriodData.totalMembers }}</text>
					</view>
				</view>
				<view class="ranking-item">
					<text class="ranking-label">销量排名</text>
					<view class="ranking-value">
						<text class="ranking-number">{{ currentPeriodData.userSort2 }}</text>
						<text class="ranking-total">/{{ currentPeriodData.totalMembers }}</text>
					</view>
				</view>
				<view class="ranking-item">
					<text class="ranking-label">成员增长排名</text>
					<view class="ranking-value">
						<text class="ranking-number">{{ currentPeriodData.userSort3 }}</text>
						<text class="ranking-total">/{{ currentPeriodData.totalMembers }}</text>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				hasSearched: false,
				// 当前时间周期
				currentPeriod: 'month',
				// 时间周期选项
				timePeriods: [{
						label: '本月',
						value: 'month'
					},
					{
						label: '本季',
						value: 'quarter'
					},
					{
						label: '本年',
						value: 'year'
					}
				],
				currentPeriodData: {
					user_level_sort: 0,
					user_level_desc: '',
					direct_usernum: 0,
					team_usernum: 0,
					direct_salenum: 0,
					team_salenum: 0,
					direct_money: 0.00,
					team_money: 0.00,
					direct_addnum: 0,
					team_addnum: 0,
					userSort1: 0,
					userSort2: 0,
					userSort3: 0,
					totalMembers: 0,
					totalCommission: 0.00,
					settleCommission: 0.00,
					transitCommission: 0.00,
					amountGrowth: 0,
					salesGrowth: 0,
					addnumGrowth: 0,
				},
				chartType: 'sales',
				chartData: {
					month: {
						sales: [],
						amount: []
					},
					quarter: {
						sales: [],
						amount: []
					},
					year: {
						sales: [],
						amount: []
					},
				},
			}
		},
		computed: {
			// 当前周期标签
			currentPeriodLabel() {
				const period = this.timePeriods.find(p => p.value === this.currentPeriod)
				return period ? period.label : '本月'
			},
			// 当前图表数据
			currentChartData() {
				return this.chartData?.[this.currentPeriod][this.chartType] || []
			}
		},
		onLoad(options) {
			this.loadDataList();
			this.loadEChart();
		},
		methods: {
			loadDataList() {
				// 加载数据列表
				if (this.hasSearched) {
					return false
				}

				this.hasSearched = true
				this.$api.fenxiao.GetUserPerformance({
					dateType: this.currentPeriod
				}).then(res => {
					this.hasSearched = false
					if (res.data.userPerformance && res.data.userPerformance.id) {
						this.currentPeriodData = res.data.userPerformance
						this.currentPeriodData.userSort1 = res.data.userSort1
						this.currentPeriodData.userSort2 = res.data.userSort2
						this.currentPeriodData.userSort3 = res.data.userSort3
						this.currentPeriodData.totalMembers = res.data.totalMembers

						this.currentPeriodData.settleCommission = res.data.settleCommission
						this.currentPeriodData.transitCommission = res.data.transitCommission
						this.currentPeriodData.totalCommission = parseFloat(res.data.settleCommission) +
							parseFloat(res.data.transitCommission)
					}
				})
			},

			// 获取个人业绩趋势图（本月4周/本季度3个月/本年4个季度）
			loadEChart() {
				this.$api.fenxiao.GetUserPerformanceEChart().then(res => {
					this.chartData = res.data
				})
			},

			// 输出: ['2024-08', '2024-09', ... , '2025-07']
			getLast6MonthsShort() {
				const months = [];
				const now = new Date();
				now.setDate(1);

				for (let i = 0; i < 6; i++) {
					const targetDate = new Date(now);
					targetDate.setMonth(now.getMonth() - i);

					const year = targetDate.getFullYear();
					const month = (targetDate.getMonth() + 1).toString().padStart(2, '0');
					months.unshift(`${year}-${month}`);
				}

				return months;
			},

			// 切换图表类型
			switchChart(type) {
				this.chartType = type
			},

			// 获取柱状图高度
			getBarHeight(value) {
				const maxValue = Math.max(...this.currentChartData.map(item => item.value))
				return maxValue > 0 ? (value / maxValue) * 100 : 0
			},

			// 格式化图表数值
			formatChartValue(value) {
				if (this.chartType === 'amount') {
					return value >= 1000 ? `${(value / 1000).toFixed(1)}k` : value.toString()
				}
				return value.toString()
			},

			// 切换时间周期
			switchPeriod(period) {
				this.currentPeriod = period
			},

			// 获取趋势文本
			getTrendText(growth) {
				if (growth > 0) {
					return '↗'
				} else if (growth < 0) {
					return '↘'
				} else {
					return '→'
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.performance-container {
		background-color: #f8f9fa;
		min-height: 100vh;
		padding: 20rpx;
		padding-bottom: calc(20rpx + env(safe-area-inset-bottom));
	}

	/* 时间筛选 */
	.time-filter {
		margin-bottom: 20rpx;
	}

	.filter-tabs {
		display: flex;
		background-color: white;
		border-radius: 20rpx;
		padding: 8rpx;
		box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.08);
	}

	.filter-tab {
		flex: 1;
		display: flex;
		align-items: center;
		justify-content: center;
		padding: 20rpx 16rpx;
		border-radius: 16rpx;
		transition: all 0.3s ease;
	}

	.filter-tab.active {
		background-color: #52a58a;
	}

	.tab-text {
		font-size: 28rpx;
		color: #666;
	}

	.filter-tab.active .tab-text {
		color: white;
		font-weight: bold;
	}

	/* 业绩概览 */
	.overview-card {
		background: linear-gradient(135deg, #52a58a 0%, #6bb6a3 100%);
		border-radius: 16rpx;
		padding: 30rpx;
		margin-bottom: 20rpx;
		color: white;
	}

	.overview-header {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 30rpx;
	}

	.overview-title {
		font-size: 32rpx;
		font-weight: bold;
	}

	.overview-date {
		font-size: 24rpx;
		opacity: 0.9;
	}

	.overview-stats {
		display: flex;
		justify-content: space-around;
	}

	.stat-item {
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	.stat-number {
		font-size: 36rpx;
		font-weight: bold;
		margin-bottom: 8rpx;
	}

	.stat-label {
		font-size: 24rpx;
		opacity: 0.8;
		margin-bottom: 12rpx;
	}

	.stat-trend {
		display: flex;
		align-items: center;
		padding: 4rpx 12rpx;
		border-radius: 12rpx;
		background-color: rgba(255, 255, 255, 0.2);
	}

	.trend-text {
		font-size: 20rpx;
		margin-right: 8rpx;
	}

	.trend-percent {
		font-size: 20rpx;
		font-weight: bold;
	}

	/* 通用卡片样式 */
	.detail-card,
	.income-card,
	.chart-card,
	.ranking-card {
		background-color: white;
		border-radius: 16rpx;
		padding: 30rpx;
		margin-bottom: 20rpx;
		box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.08);
	}

	.detail-header,
	.income-header,
	.chart-header,
	.ranking-header {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 30rpx;
	}

	.detail-title,
	.income-title,
	.chart-title,
	.ranking-title {
		font-size: 32rpx;
		font-weight: bold;
		color: #333;
	}

	/* 详细数据 */
	.detail-grid {
		display: grid;
		grid-template-columns: repeat(2, 1fr);
		gap: 30rpx;
	}

	.detail-item {
		display: flex;
		flex-direction: column;
		align-items: center;
		padding: 30rpx 20rpx;
		background-color: #f8f9fa;
		border-radius: 12rpx;
	}

	.detail-label {
		font-size: 24rpx;
		color: #666;
		margin-bottom: 12rpx;
	}

	.detail-value {
		font-size: 32rpx;
		font-weight: bold;
		color: #52a58a;
	}

	/* 收益明细 */
	.income-total {
		font-size: 24rpx;
		color: #52a58a;
		font-weight: bold;
	}

	.income-list {}

	.income-item {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 24rpx 0;
		border-bottom: 1rpx solid #f0f0f0;
	}

	.income-item:last-child {
		border-bottom: none;
	}

	.income-info {
		flex: 1;
	}

	.income-type {
		font-size: 28rpx;
		color: #333;
		font-weight: bold;
		margin-bottom: 8rpx;
		display: block;
	}

	.income-desc {
		font-size: 24rpx;
		color: #999;
	}

	.income-amount {
		font-size: 28rpx;
		color: #52a58a;
		font-weight: bold;
	}

	/* 图表区域 */
	.stats-card {
		background-color: white;
		border-radius: 20rpx;
		padding: 30rpx;
		margin-bottom: 20rpx;
	}

	.card-header {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 30rpx;
	}

	.card-title {
		font-size: 32rpx;
		font-weight: 600;
		color: #333;
	}

	.chart-tabs {
		display: flex;
		gap: 10rpx;
	}

	.chart-tab {
		padding: 8rpx 20rpx;
		background-color: #f8f9fa;
		border-radius: 20rpx;
		border: 2rpx solid transparent;
	}

	.chart-tab.active {
		background-color: #e8f5e8;
		border-color: #52a58a;
	}

	.chart-tab-text {
		font-size: 24rpx;
		color: #333;
	}

	.chart-tab.active .chart-tab-text {
		color: #52a58a;
		font-weight: 600;
	}

	/* 简化图表 */
	.simple-chart {
		margin-top: 30rpx;
	}

	.chart-bars {
		display: flex;
		align-items: flex-end;
		gap: 20rpx;
		height: 200rpx;
		padding: 20rpx 0;
	}

	.chart-bar {
		flex: 1;
		display: flex;
		flex-direction: column;
		align-items: center;
		height: 100%;
	}

	.bar-container {
		flex: 1;
		width: 40rpx;
		background-color: #f0f0f0;
		border-radius: 20rpx;
		position: relative;
		display: flex;
		align-items: flex-end;
	}

	.bar-fill {
		width: 100%;
		background: linear-gradient(180deg, #52a58a 0%, #6bb6a3 100%);
		border-radius: 20rpx;
		min-height: 10rpx;
	}

	.bar-label {
		font-size: 22rpx;
		color: #666;
		margin-top: 10rpx;
	}

	.bar-value {
		font-size: 20rpx;
		color: #52a58a;
		font-weight: 600;
		margin-top: 4rpx;
	}

	/* 排名信息 */
	.ranking-list {}

	.ranking-item {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 24rpx 0;
		border-bottom: 1rpx solid #f0f0f0;
	}

	.ranking-item:last-child {
		border-bottom: none;
	}

	.ranking-label {
		font-size: 28rpx;
		color: #333;
	}

	.ranking-value {
		display: flex;
		align-items: baseline;
	}

	.ranking-number {
		font-size: 32rpx;
		color: #52a58a;
		font-weight: bold;
	}

	.ranking-total {
		font-size: 24rpx;
		color: #999;
		margin-left: 4rpx;
	}
</style>